<?php


namespace app\erp\model;


use think\Db;
use think\Model;

class Brandcertdb extends Model
{
    protected $pk = 'id';
    protected $name = 'brandcert';
    protected $append = ['card','area_arr','category_arrs','state_tips','category_arr_view','supplier_company'];

    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i',
        'audit_time' => 'timestamp:Y-m-d H:i'
    ];

    protected $autoWriteTimestamp = true;

    public $param = [];

    public function base($query){
        if(isset($this->param['supplier_id']) && $this->param['supplier_id']){
            $query->where('supplier_id',$this->param['supplier_id']);
        }
        if(isset($this->param['brand_name']) && $this->param['brand_name']){
            $query->whereLike('brand_name','%'.$this->param['brand_name'].'%');
        }

        // 状态搜索
        if(isset($this->param['audit_state']) && $this->param['audit_state']){
            $query->where('audit_state',$this->param['audit_state']);
        }

        // 管理部门搜索
        if(isset($this->param['department']) && $this->param['department']){
            $query->where('department',$this->param['department']);
        }

        // 申请时间开始
        if(isset($this->param['min_create_time']) && $this->param['min_create_time']){
            $query->whereTime('create_time', '>=', $this->param['min_create_time']);
        }
        if(isset($this->param['max_create_time']) && $this->param['max_create_time']){
            $time = strtotime($this->param['max_create_time']);
            $date = strtotime('+1 day', $time);
            $query->whereTime('create_time', '<', $date);
        }
        // 申请时间结束

        // 审核时间开始
        if(isset($this->param['min_audit_time']) && $this->param['min_audit_time']){
            $query->whereTime('audit_time', '>=', $this->param['min_audit_time']);
        }
        if(isset($this->param['max_audit_time']) && $this->param['max_audit_time']){
            $time = strtotime($this->param['max_audit_time']);
            $date = strtotime('+1 day', $time);
            $query->whereTime('audit_time', '<', $date);
        }
        // 申请时间结束

        // 授权开始时间开始
        if(isset($this->param['min_start_date']) && $this->param['min_start_date']){
            $query->whereTime('start_date', '>=', $this->param['min_start_date']);
        }
        if(isset($this->param['max_start_date']) && $this->param['max_start_date']){
            $time = strtotime($this->param['max_start_date']);
            $date = strtotime('+1 day', $time);
            $query->whereTime('start_date', '<', $date);
        }
        // 授权开始时间结束

        // 授权开始时间开始
        if(isset($this->param['min_end_date']) && $this->param['min_end_date']){
            $query->whereTime('end_date', '>=', $this->param['min_end_date']);
        }
        if(isset($this->param['max_end_date']) && $this->param['max_end_date']){
            $time = strtotime($this->param['max_end_date']);
            $date = strtotime('+1 day', $time);
            $query->whereTime('end_date', '<', $date);
        }
        // 授权开始时间结束
    }

    /**
     * Title("授权来源 供应商公司名称")
     */
    public function getSupplierCompanyAttr($val,$row){
        if($row['supplier_id']){
            $get_member =  Db::name('member')->where('member_id',$row['supplier_id'])->field('company_name,username')->find();
            return $get_member['company_name']?$get_member['company_name']:$get_member['username'];
        }
        return '';
    }

    /**
     * Title("获取证书编号")
     */
    public function getCardAttr($value,$row){
        $card = str_pad($row['sn'],5,'0',STR_PAD_LEFT);
        if($row['son_sn']>0){
            $card = $card.'-'.$row['son_sn'];
        }
        return $card;
    }

    /**
     * Title("获取分类数组")
     */
    public function getCategoryArrsAttr($val,$row){
        if($row['category_arr'] == 'all'){
            $result = [];
            array_push($result,['category_name'=>'全品类','category_sign'=>'all']);
            return $result;
        } else {
            return Db::name('category')->whereIn('category_sign',explode(',',$row['category_arr']))->field('category_name,category_sign')->select();
        }
        //return explode(',',$row['category_arr']);
    }
    /**
     * Title("获取分类数组")
     */
    public function getCategoryArrViewAttr($val,$row){
        if($row['category_arr'] == 'all'){
            return '全品类';
        } else {
            $result = Db::name('category')->whereIn('category_sign',explode(',',$row['category_arr']))->column('category_name');
            return implode(',',$result);
        }
        //return explode(',',$row['category_arr']);
    }

    /**
     * Title("获取地区数组")
     */
    public function getAreaArrAttr($val,$row){
        return explode(',',$row['area']);
    }

    /**
     * Title("获取渠道")
     */
    public function getChannelAttr($val){
        return explode(',',$val);
    }

    /**
     * Title("设置渠道")
     */
    public function setChannelAttr($val){
        return implode(',',$val);
    }

    /**
     * Title("处理文件读写")
     */
    public function getFilesAttr($val){
        $base = explode(',',$val);
        $result = [];
        if($val) {
            for ($i = 0; $i < count($base); $i++) {
                $t1 = explode('/', $base[$i]);
                $temp['file_name'] = end($t1);
                $temp['save_path'] = $base[$i];
                array_push($result, $temp);
            }
        }
        return $result;
    }
    public function setFilesAttr($val){
        return implode(',',$val);
    }

    /**
     * Title("处理证书读写")
     */
    public function getCertsAttr($val){
        $base = explode(',',$val);
        $result = [];
        if($val) {
            for ($i = 0; $i < count($base); $i++) {
                $t1 = explode('/', $base[$i]);
                $temp['file_name'] = end($t1);
                $temp['save_path'] = $base[$i];
                array_push($result, $temp);
            }
        }
        return $result;
    }
    public function setCertsAttr($val){
        return implode(',',$val);
    }

    /**
     * Title("到期提醒")
     */
    public function getStateTipsAttr($val,$row){
        $diff_day = (strtotime($row['end_date']) - time())/86400;
        if($diff_day < 0 ){
            return 4; // 过期
        } elseif($diff_day < 30){
            return 3;
        } elseif($diff_day < 90){
            return 2;
        } else {
            return 1;
        }
    }


}